'''
配置文件控制类
'''
import json,os


class ConfigUtil():
    def __init__(self,path):
        path=path+"/config"
        if not os.path.exists(path):
            os.mkdir(path)
            

        self.file=path+"/data.json"
        self.encode='utf-8'
        if not os.path.exists(self.file):
            data={"start":0,"step":10,'state':0,"showMain":0,'openIcon':0}
            self.write(data)


    def read(self):
        '''
        读取内容
        '''
        if not os.path.exists(self.file):
            return None
        
        else:
            with open(self.file,'r',encoding=self.encode) as load_f:
                load_dict = json.load(load_f)
            return load_dict

    def write(self,data):
        '''
        写入内容
        '''
        with open(self.file,"w",encoding=self.encode) as f:
            json.dump(data,f,ensure_ascii=False,sort_keys=True, indent=4, separators=(',', ': '))
        

if __name__ == "__main__":
    conf=ConfigUtil()
    data={"时间":["a","b","c"],"结构":["add",'ddd','ccc']}
    conf.write(data)
    result=conf.read()
    print("完成读取")
    if result:
        for one in result:
            print(one,result[one])
            for two in result[one]:
                print(two)
    else:
        print("结果为空")